package com.duowei.diandan.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.duowei.diandan.R;
import com.duowei.diandan.adapter.CommonFoodAdapter;
import com.duowei.diandan.app.OrderList;
import com.duowei.diandan.bean.food.Single;
import com.duowei.diandan.bean.food.SingleAndMeals;
import com.duowei.diandan.bean.food.SingleGroup;
import com.duowei.diandan.constant.Settings;
import com.duowei.diandan.databinding.GroupFragmentBinding;
import com.duowei.diandan.db.FoodMenu;
import com.duowei.diandan.event.ToastEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.Observable;
import java.util.Observer;

/**
 * 单品组
 */
public class SingleGroupFragment extends Fragment {
    public static final String TAG = "SingleGroupFragment";

    private GroupFragmentBinding mBinding;

    private SingleGroup mSingleGroup;

    private CommonFoodAdapter mAdapter;

    private Observer mDataUpdateObserver = new Observer() {
        @Override
        public void update(Observable o, Object arg) {
            if (mSingleGroup != null) {
                FoodMenu.getInstance().querySingleGroup(mSingleGroup.getNo(), singleGroup -> {
                    if (singleGroup != null) {
                        mSingleGroup = singleGroup;
                        mBinding.titleView.setText(mSingleGroup.getName());

                        mAdapter.update(mSingleGroup.getFoodList());

                    } else {
                        if (mOnClickListener != null) {
                            mOnClickListener.onCloseClick();
                        }
                    }
                });
            }
        }
    };

    public static SingleGroupFragment newInstance(SingleGroup singleGroup) {

        Bundle args = new Bundle();
        args.putSerializable("singleGroup", singleGroup);

        SingleGroupFragment fragment = new SingleGroupFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mSingleGroup = (SingleGroup) getArguments().getSerializable("singleGroup");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_single_group, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        mBinding.titleView.setText(mSingleGroup.getName());

        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), Settings.COLUMN_SINGLE_GROUP,
                GridLayoutManager.VERTICAL, false);
        mBinding.recyclerView.setLayoutManager(layoutManager);
        mBinding.recyclerView.setHasFixedSize(true);

        mAdapter = new CommonFoodAdapter(mSingleGroup.getFoodList());
        mAdapter.setOnItemClickListener(food -> {
            if (food instanceof Single) {
                Single single = (Single) food;

                if (single.hasPresentOrAddPrice()) {
                    if (mOnClickListener != null) {
                        mOnClickListener.onHasPresentOrAddPrice(single);
                    }

                } else {
                    EventBus.getDefault().post(new ToastEvent(single.getName() + " +1"));
                    OrderList.getInstance().addSingle(single, null, null);

                    if (mOnClickListener != null) {
                        mOnClickListener.onCloseClick();
                    }
                }

            } else if (food instanceof SingleAndMeals) {
                SingleAndMeals singleAndMeals = (SingleAndMeals) food;

                if (mOnClickListener != null) {
                    mOnClickListener.onSingleAndMeals(singleAndMeals);
                }
            }
        });
        mBinding.recyclerView.setAdapter(mAdapter);

        mBinding.backButton.setOnClickListener(v -> {
            if (mOnClickListener != null) {
                mOnClickListener.onCloseClick();
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        FoodMenu.getInstance().addObserver(mDataUpdateObserver);
    }

    @Override
    public void onStop() {
        super.onStop();
        FoodMenu.getInstance().deleteObserver(mDataUpdateObserver);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    private OnClickListener mOnClickListener;

    public interface OnClickListener {
        void onHasPresentOrAddPrice(Single single);

        void onSingleAndMeals(SingleAndMeals singleAndMeals);

        void onCloseClick();
    }
}
